package cn.txun.csmall.sentinal.study;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class SentinelStudyApplication {
    public static void main(String[] args) {
        SpringApplication.run(SentinelStudyApplication.class);
        //编写保护sayHi资源的流控规则/熔断规则
/*
        List<DegradeRule> rules = new ArrayList<>();
        //创建具体的熔断规则，放在list中
        DegradeRule degradeRule = new DegradeRule();
        //规则对应限制的资源id
        degradeRule.setResource("sayHi");
        //设置调用时间超过1毫秒
        degradeRule.setCount(1);
        //设置慢调用
        degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
        degradeRule.setTimeWindow(10);//如果出现熔断，10秒后重试
        degradeRule.setSlowRatioThreshold(0.5);//慢调用比例阈值，达到0.5的比例
        degradeRule.setMinRequestAmount(1);//触发熔断最小请求数量
        degradeRule.setStatIntervalMs(10000);//统计的时间范围
        rules.add(degradeRule);
        //将熔断规则列表加载到程序内容中
        DegradeRuleManager.loadRules(rules);*/

        //针对资源的规则，一个程序可以有多个规则对象，流控规则FlowRule
      /*  List<FlowRule> rules = new ArrayList<>();
        //创建具体的流控规则，放在list中
        FlowRule flowRule = new FlowRule();
        flowRule.setRefResource("sayHi");
        //设置qps上限为1
        flowRule.setCount(1);
        //设置限流阈值类型 1=qps  0=并发
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //总结上述规则表示的含义：对sayHi的资源，设置一个流控规则，qps数字超过1/s实行限流
        //并抛出调用资源的异常BlockException
        rules.add(flowRule);
        //将规则列表加载到程序内容中
        FlowRuleManager.loadRules(rules);*/
    }
}
